using System;
using System.Collections;
using System.Security.Principal;
using System.Web;
using System.Web.Profile;
using System.Web.SessionState;

namespace WebMonitor.Http.Impl {
    /// <summary>
    /// Summary description for HttpContextAdapter.
    /// </summary>
    public class HttpContextAdapter : IHttpContext {
        private readonly HttpContext ctx = HttpContext.Current;

        public HttpContextAdapter() {}

        public HttpContextAdapter(HttpContext ctx) {
            this.ctx = ctx;
        }

        public object GetService(Type serviceType) {
            return ((IServiceProvider) ctx).GetService(serviceType);
        }

        public void AddError(Exception errorInfo) {
            ctx.AddError(errorInfo);
        }

        public void ClearError() {
            ctx.ClearError();
        }

        public object GetSection(string name) {
            return ctx.GetSection(name);
        }

        public void RewritePath(string path) {
            ctx.RewritePath(path);
        }

        public void RewritePath(string filePath, string pathInfo, string queryString) {
            ctx.RewritePath(filePath, pathInfo, queryString);
        }

        public IHttpApplication ApplicationInstance {
            get { return new HttpApplicationAdapter(ctx.ApplicationInstance); }
        }

        public IHttpApplicationState Application {
            get { return new HttpApplicationStateAdapter(ctx.Application); }
        }

        public IHttpHandler Handler {
            get { return ctx.Handler; }
            set { ctx.Handler = value; }
        }

        public IHttpRequest Request {
            get { return new HttpRequestAdapter(ctx.Request); }
        }

        public IHttpResponse Response {
            get { return new HttpResponseAdapter(ctx.Response); }
        }

        public TraceContext Trace {
            get { return ctx.Trace; }
        }

        public IDictionary Items {
            get { return ctx.Items; }
        }

        public IHttpSessionState Session {
            get { return new HttpSesssionStateAdapter(ctx.Session); }
        }

        public IHttpServerUtility Server {
            get { return new HttpServerUtilityAdapter(ctx.Server); }
        }

        public Exception Error {
            get { return ctx.Error; }
        }

        public Exception[] AllErrors {
            get { return ctx.AllErrors; }
        }

        public IPrincipal User {
            get { return ctx.User; }
            set { ctx.User = value; }
        }

        public bool SkipAuthorization {
            get { return ctx.SkipAuthorization; }
            set { ctx.SkipAuthorization = value; }
        }

        public bool IsDebuggingEnabled {
            get { return ctx.IsDebuggingEnabled; }
        }

        public bool IsCustomErrorEnabled {
            get { return ctx.IsCustomErrorEnabled; }
        }

        public DateTime Timestamp {
            get { return ctx.Timestamp; }
        }

        public ICache Cache {
            get { return new CacheAdapter(ctx.Cache); }
        }

        public ProfileBase Profile {
            get { return ctx.Profile; }
        }
    }
}